package sen.queryUtils;

import java.util.Iterator;
import java.util.List;

public class W {

	/*******分页与排序的参数名---start********/
	private final static String COL = "col";
	private final static String ORDER = "order";
	private final static String ROWSTART = "rowStart";
	private final static String PAGESIZE = "pageSize";
	/*******分页与排序的参数名---end********/
	
	private String whereStr = "";
	private String orderStr = "";
	private int rowStart = 0;
	private int pageSize = 0;
	
	public boolean ifPaging(){
		return rowStart>=0 && pageSize>0;
	}
	public static W getW(List<Cond> conds){
		if(conds==null || conds.isEmpty()) {
			return new W();
		}
		String col = null, order = null;
		int rowStart = 0, pageSize = 0;
		StringBuilder w = new StringBuilder();
		boolean firstOr = true;
		for (Iterator<Cond> it = conds.iterator(); it.hasNext();) {
			Cond cond = it.next();
			
			if(cond instanceof Connector) {
				//如果当前不是左括号则删除最后的and
				if(!(((Connector) cond).getName().equals(Connector.LEFT_BRACKET.getName()))){
					delLastAnd(" and ", w);
				}
				Connector connector = (Connector)cond;
				w.append(" "+connector.getName()+" ");
				continue;
			}
			Conditional conditional = null;
			if(cond instanceof Conditional) {
				conditional = (Conditional)cond;
			}
			String name = conditional.getName();
			String value = conditional.getValue();
			int relator = conditional.getRelator();
			if(COL.equals(name)) {
				col = value;
				continue;
			} else if(ORDER.equals(name)) {
				order = value;
				continue;
			} else if(ROWSTART.equals(name)) {
				try {
					rowStart = Integer.parseInt(value);
				} catch (NumberFormatException e) {
				}
				continue;
			} else if(PAGESIZE.equals(name)) {
				try {
					pageSize = Integer.parseInt(value);
				} catch (NumberFormatException e) {
				}
				continue;
			}
			
			String value2 = "'"+value+"'";
			String item = "";
			switch (relator) {
			case Conditional.GT:
				item = name + ">" + value2;
				break;
			case Conditional.LT:
				item = name + "<" + value2;
				break;
			case Conditional.EQ:
				if(value == null) {
					item = name + " IS NULL";
				} else {
					item = name + "=" + value2;
				}
				break;
			case Conditional.GT_EQ:
				item = name + ">=" + value2;
				break;
			case Conditional.LT_EQ:
				item = name + "<=" + value2;
				break;
			case Conditional.START:
				item = name + " like '" + value + "%'";
				break;
			case Conditional.END:
				item = name + " like '%" + value + "'";
				break;
			case Conditional.DIM:
				item = name + " like '%" + value + "%'";
				break;
			case Conditional.L_DIM:
				item = name + " like '%" + value + "'";
				break;
			case Conditional.R_DIM:
				item = name + " like '" + value + "%'";
				break;
			case Conditional.SCOPE://如果是范围，则值应该是以逗号分隔
				String[] tmp = value.split(Conditional.SCOPE_SEPARATE);
				try {
					item = MyUtils.getOrStr(tmp, name, true);
				} catch (Exception e) {
				}
				break;
			case Conditional.NO_EQ:
				if(value == null) {
					item = name + " IS NOT NULL";
				} else {
					item = name + " !=" + value2;
				}
				break;
			case Conditional.ONE_OF_THEM:
				item = "(:n=':v' or :n like ':v,%' or :n like '%,:v,%' or :n like '%,:v')";
				item = item.replace(":n", name).replace(":v", value);
				break;
			}
			if(cond instanceof ConditionalOr) {
				if(firstOr) {
					w.append("(");
					firstOr = false;
				}
				w.append(item + " or ");
			} else {
				if(!firstOr) {//如果or关系的条件已经结束了，去掉末尾多余的or，加回括号
					w.delete(w.lastIndexOf("or"), w.length());
					w.append(") and ");
					firstOr = true;
				}
				w.append(item + " and ");
			}
		}
		if(!firstOr) {//如果一直没有and出现则表明全是or，去掉末尾多余的or，加回括号
			w.delete(w.lastIndexOf("or"), w.length());
			w.append(")");
			firstOr = true;
		}
		
		if(!"".equals(w.toString())) {
			delLastAnd(" and ", w);
			w.insert(0, " where ");
		}
		
		W ww =  new W();
		ww.setWhereStr(w.toString());
		ww.setOrderStr(getOrderStr(col, order));
		ww.setPageSize(pageSize);
		ww.setRowStart(rowStart);
		return ww;
	}
	private static String getOrderStr(String col, String order) {
		String orderStr = "";
		StringBuilder orderStrTemp = new StringBuilder();
		if(null!=col&&!"".equals(col) && null!=order&&!"".equals(order)){
			String[] cs = col.split(",");
			String[] os = order.split(",");
			for (int i = 0; i < os.length; i++) {
				orderStrTemp.append(cs[i] + " "+os[i] + ",");
			}
			MyUtils.delLast(orderStrTemp, ",", null, null);
			if(orderStrTemp.length()>0){
				orderStr = " order by " + orderStrTemp.toString();
			}
		}
		return orderStr;
	}
	public String getWhereStr() {
		return whereStr;
	}
	public void setWhereStr(String whereStr) {
		this.whereStr = whereStr;
	}
	public String getOrderStr() {
		return orderStr;
	}
	public void setOrderStr(String orderStr) {
		this.orderStr = orderStr;
	}
	public int getRowStart() {
		return rowStart;
	}
	public void setRowStart(int rowStart) {
		this.rowStart = rowStart;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	private static boolean delLastAnd(String word, StringBuilder s){
		int index = s.lastIndexOf(word);
		if(index>0 && index==s.length()-word.length()) {
			s.delete(index, s.length());
		} else {
			return false;
		}
		return true;
	}
}
